﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KylinORM.Message
{
    /// <summary>
    /// 消息服务器连接参数
    /// </summary>
    public struct MessageServerConnectionString
    {
        #region 分析连接字符串
        
        private string connectionString;
        private Dictionary<string,string> parms;
        /// <summary>
        /// 链接到指定的消息服务器
        /// </summary>
        /// <param name="connectionString"></param>
        public MessageServerConnectionString(string connectionString)
        {
            this.connectionString = connectionString;
            this.parms = new Dictionary<string,string>();
            //分析连接字符串
            foreach(string parm in  this.connectionString.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                addParm(parm, '=');
            }
            //拆分主机地址和端口号
            if (this.parms["server"] != null)
            {
                var serverParms = this.parms["server"].Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (serverParms.Length > 0)
                    addParm("host", serverParms[0]);
                if (serverParms.Length > 1)
                    addParm("port", serverParms[1]);
            }
        }
        /// <summary>
        /// 将指定键值添加到字典，键值使用separator分割
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="separator"></param>
        private void addParm(string keyValue, char separator)
        {
            if (keyValue == null)
                return;
            string[] kv = keyValue.Split(new char[] { separator }, StringSplitOptions.RemoveEmptyEntries);
            if (kv.Length != 2)
                return;
            addParm(kv[0], kv[1]);
        }
        /// <summary>
        /// 将指定键值添加到字典
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private void addParm(string key, string value)
        {
            if (key == null || value == null)
                return;
            parms.Add(key.Trim(), value.Trim());
        }
        /// <summary>
        /// 得到指定键的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private string getParm(string key)
        {
            if (parms.ContainsKey(key))
                return parms[key];
            else
                return null;
        }
        #endregion

        /// <summary>
        /// 主机地址
        /// </summary>
        public string Host 
        {
            get { return getParm("host"); } 
        }
        /// <summary>
        /// 端口
        /// </summary>
        public int? Port
        {
            get { return getParm("port") == null ? (int?)null : Convert.ToInt32(getParm("port")); }
        }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName {
            get { return getParm("username"); }
        }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password 
        {
            get { return getParm("password"); }
        }
        /// <summary>
        /// 数据库
        /// </summary>
        public string Database
        {
            get { return getParm("database"); }
        }
    }
}
